package com.auction.job.core.service.impl;

import com.auction.job.core.schedule.QuartzManager;
import com.auction.job.core.service.BaseService;
import com.auction.job.core.service.JobManagerService;
import com.auction.job.domain.request.DupBuyJobRequest;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yisu.auction.ewf.common.web.HttpWebClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by nichole on 2017/8/17.
 */
@Service("jobManagerService")
public class JobManagerServiceImpl extends BaseService implements JobManagerService {

    @Override
    public boolean addJob(String prizeId) {
        return QuartzManager.addPurchaseJob(prizeId);
    }

    @Override
    public boolean addOpenJob(String prizeId, String lastPurchaseTime) {
//        return QuartzManager.addOpenPrizeJob(prizeId);

        final String tempPrizeId = prizeId;
        final String tempLastPurchaseTime = lastPurchaseTime;
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(9);
                } catch (Exception e) {
                    LOG.error("Thread was interrupted, cause: {}", e);
                }
                Map<String, String> params = new HashMap<>();
                params.put("prizeId", tempPrizeId);
                params.put("lastPurchaseTime", tempLastPurchaseTime);
                String result = HttpWebClient.post(systemConfig.getOpenPrizeUrl(), params);
                LOG.info("open prize response for prize[{}]: {}", tempPrizeId, result);
                JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                int errorCode = jsonObject.get("errorCode").getAsInt();
                if (errorCode == 0) {
                    LOG.info("successfully opened prize for prize[{}]", tempPrizeId);
                    QuartzManager.removePurchaseJob(tempPrizeId);
                    int nextPrizeId = jsonObject.get("data").getAsInt();
                    if (nextPrizeId > 0) {
                        QuartzManager.addPurchaseJob(String.valueOf(nextPrizeId));
                    }
                } else {
                    LOG.error("failed to open prize for prize[{}], error code[{}], error message[{}]",
                            new String[]{tempPrizeId, String.valueOf(errorCode), jsonObject.get("errorMessage").getAsString()});
                }
            }
        });
        thread.start();
        return true;
    }

    @Override
    public boolean removeJob(String prizeId) {
        return QuartzManager.removePurchaseJob(prizeId);
    }

    @Override
    public boolean addDupBuyJob(DupBuyJobRequest jobRequest) {
        return QuartzManager.addDupBuyJob(jobRequest.getPrizeId(), jobRequest.getUserId());
    }

    @Override
    public boolean addResumeErrorJob() {
        return QuartzManager.addResumeErrorJob();
    }

    @Override
    public boolean removeOpenJob(String prizeId) {
        return QuartzManager.removeOpenPrizeJob(prizeId);
    }


    @Override
    public boolean addArchiveJob() {
        return QuartzManager.addArchiveJob();
    }

    @Override
    public boolean removeArchiveJob() {
        return QuartzManager.removeArchiveJob();
    }

    @Override
    public boolean addPushJob() {
        return QuartzManager.addPushJob();
    }

    @Override
    public boolean addReportDataJob() {
        return QuartzManager.addReportDataJob();
    }
}
